<?php
require_once dirname(__FILE__) . '/common.inc.php';

if (isset($_COOKIE[USER])) {
  $_SESSION[USER] = json_decode($_COOKIE[USER]);
}

$is_customised = MiscUtils::getParam('c', 0);
$template = MiscUtils::getParam('t', 'index');
$options = MiscUtils::getParam('opts', json_encode(array()));
$base = MiscUtils::getParam('b', 'base');

$smarty->assign('p', 'ui/default');
$smarty->assign('t', "$template");
$smarty->assign('u', json_encode((isset($_SESSION[USER])) ? $_SESSION[USER] : array()));
$smarty->assign('d', time());
$smarty->assign('opts', urldecode($options));


$menuInst = new stdClass();
$menuInst->id = 0;
$menuInst->name = 'ROOT';
$menuInst->link = '';
$menuInst->c = array();
_pages(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, $menuInst);
$smarty->assign('menuInst', $menuInst);

$message = array();
$message['m'] = MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d, NULL);

//TEMPORARY ADDED
$smarty->assign('mess', $message['m']);

if ($is_customised == 1) {
  $smarty->assign('p', 'custom/ui/gui/html');
  //MUST CREATE /custom/data.php
  require_once dirname(__FILE__) . '/custom/data.php';
  loadPage_custom($template, $message);
} else {
  loadPage($template, $message);
}
$smarty->display("ui/default/$base.tpl");
return;

function loadPage($template, $message) {
  switch ($template) {
    case 'index':
      index($message);
      break;
    default:
      header('Location: http://' . DOMAIN . PATH);
      break;
  }
}

function _pages($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->link = '?i=' . $tmp->id;
      $tmp->c = array();
      $node->c[] = $tmp;
      _pages($arr, $tmp);
    }
  }
}

function index($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', 1);
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $message[0] = DbUtils::get(DbUtils::createPdoInst(), 'pz_page', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    $content = (count($message[0]->d) == 0) ? '' : $message[0]->d[0]->content;
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = :v1';
    $cond_vals->v = array(':v1' => $id);
    $opt = new stdClass();
    $opt->select_expr = 't.*, t1.name, t1.folder';
    $opt->join_expr = 'LEFT JOIN pz_plugin AS t1 ON t1.id = t.plugin_id';
    $instances = DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin_instance', $cond_vals, NULL, NULL, NULL, NULL, $opt);
    
    $scripts = array();
    foreach ($instances->d as &$instance) {
      $script = _script($instance->folder, $instance->id, $instance->width, $instance->height);
      $content = str_replace("{" . $instance->folder . ": " . "$instance->id" . "}", $script, $content);
    }
    
    $message['idx'] = $id;
    $smarty->assign('msg', json_encode($message));
    $smarty->assign('content', $content);
    
    //TEMPORARY ADDED
    $smarty->assign('idx', $id);
  } else {
    $smarty->assign('content', '');
  }
}

function _script($folder, $instance_id, $width, $height) {
  return "<iframe width=\"$width\" height=\"$height\" scrolling=\"no\" frameborder=\"0\"  marginwidth=\"0\" marginheight=\"0\" src=\"plugin.php?i=$instance_id&f=$folder&s=index\"></iframe>";
}
?>